package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.impl.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @version 1.0
 * @Author 郭腾高
 * @Date 2024/9/4 20:34
 */
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;
    @GetMapping("/emps")
    //分页查询
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
                       ){
        PageBean pageBean =  empService.empList(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }
    //批量删除
    @DeleteMapping("/emps/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
           empService.empDelete(ids);
           return Result.success();
    }
    //新增员工
    @PostMapping("/emps")
    public Result save(@RequestBody Emp emp){
        empService.insert(emp);
        return Result.success();
    }
    //查询回显，编辑员工信息
    @GetMapping("/emps/{id}")
    public Result getById(@PathVariable Integer id){
        Emp empById = empService.getById(id);
        return Result.success(empById);
    }
    //修改员工
    @PutMapping("/emps")
    public Result updateById(@RequestBody Emp emp){
        empService.updateById(emp);
        return Result.success();
    }

}
